package com.woniuxy.monitor.controller;

import com.woniuxy.monitor.pojo.Monitor;
import com.woniuxy.monitor.service.impl.MonitorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.util.List;

/**
 * 流媒体控制器
 * 提供直播流转发和流列表查询功能
 */
@RestController
@RequestMapping("/stream")
@CrossOrigin
public class StreamController {

    @Autowired
    private MonitorServiceImpl monitorService;

    /**
     * 获取所有可用的直播流列表
     * @return 监控设备列表
     */
    @GetMapping("/list")
    public List<Monitor> getStreamList() {
        return monitorService.getAllActiveStreams();
    }

    /**
     * 转发指定ID的直播流
     * @param id 监控设备ID
     * @return 直播流响应
     */
    @GetMapping("/live/{id}")
    public ResponseEntity<StreamingResponseBody> forwardStream(@PathVariable Integer id) {
        return monitorService.forwardStream(id);
    }

    /**
     * 兼容旧版API，转发默认直播流（如果有）
     * @return 直播流响应
     */
    @GetMapping("/live")
    public ResponseEntity<StreamingResponseBody> forwardDefaultStream() {
        // 获取第一个可用的直播流
        List<Monitor> streams = monitorService.getAllActiveStreams();
        if (streams.isEmpty()) {
            return ResponseEntity.notFound().build();
        }
        return monitorService.forwardStream(streams.get(0).getId());
    }
}
